using System;
using System.Collections.Generic;
using System.Text;

using miciv.Misc;

namespace miciv
{
    abstract public class DataSourceBase<TT> : IDataSource where TT : class
    {
        public delegate void UpdateHandler(DataSourceBase<TT> _dataSource);

        protected class Data
        {
            protected TT m_data;

            public Data(TT _data)
            {
                this.m_data = _data;
            }

            public TT Value
            {
                get
                {
                    return this.m_data;
                }
            }
        }

        protected UpdateHandler m_UpdateHandler;
        virtual public UpdateHandler Updater
        {
            get
            {
                return this.m_UpdateHandler;
            }
            set
            {
                if (value != this.m_UpdateHandler)
                {
                    this.m_UpdateHandler = value;
                    this.Update();
                }
            }
        }

        protected List<Data> m_lstData;
        protected bool m_bHasChanged;

        public DataSourceBase()
        {
            this.m_lstData = new List<Data>();
            this.HasChanged = false;
        }

        virtual public bool Add(TT _data)
        {
            this.m_lstData.Add(new Data(_data));
            this.HasChanged = true;
            return true;
        }

        virtual public void Update()
        {
            this.Clear();
            if (null != this.m_UpdateHandler)
            {
                this.m_UpdateHandler(this);
            }
        }

        #region IDataSource Membres

        virtual public int Count
        {
            get
            {
                return this.m_lstData.Count;
            }
        }

        abstract public T GetData<T>(int _sSourceIndex, string _strFieldName) where T : class;

        virtual public void Clear()
        {
            this.m_lstData.Clear();
            this.HasChanged = true;
        }

        virtual public bool HasChanged
        {
            get
            {
                return this.m_bHasChanged;
            }
            set
            {
                this.m_bHasChanged = value;
            }
        }

        #endregion
    }
}
